package com.dazzle.system.controller;

import java.util.HashSet;
import java.util.List;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.commons.lang.StringUtils;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;

import com.dazzle.core.util.query.QueryFilter;
import com.dazzle.system.entity.SysRole;
import com.dazzle.system.entity.SysUser;
import com.dazzle.system.service.SysRoleService;
import com.dazzle.system.service.SysUserService;

import flexjson.JSONSerializer;

@Controller
@RequestMapping("/system")
public class SysUserController{
	@Resource
	private SysUserService sysUserService;
	@Resource
	private SysRoleService sysRoleService;
	
	@RequestMapping("/userlist")
	public String list(HttpServletRequest request,HttpServletResponse response){
		QueryFilter queryFilter = new QueryFilter(request);
		List<SysUser> list = sysUserService.getAll(queryFilter);
		request.setAttribute("userList", list);
		request.setAttribute("pageBean", queryFilter.getPagingBean());
		List<SysRole> rolesAll = sysRoleService.getAll();
		request.setAttribute("rolesAll", rolesAll);
		return "system/sys_user_list";
	}
	@RequestMapping("/add_sys_user")
	public String add(@ModelAttribute("SysUserForm") SysUser sysUser,HttpServletRequest request) {
		if(sysUser!=null){
			HashSet<SysRole> roles = new HashSet<SysRole>();
			String[] roleIds = request.getParameterValues("roleIds");
			if(roleIds !=null && roleIds.length>0){
				for(String roleId:roleIds){
					if(StringUtils.isNotEmpty(roleId))
						roles.add(sysRoleService.get(new Long(roleId)));
				}
			}
			sysUser.setRoles(roles);
			sysUserService.save(sysUser);
		}
		return "forward:userlist.do";
	}
	@RequestMapping("/json_get_sys_user")
	public  String get(HttpServletRequest request,HttpServletResponse response) {
		String userId = request.getParameter("editId");
		if(StringUtils.isNotEmpty(userId)){
			SysUser sysUser = sysUserService.get(new Long(userId));
			JSONSerializer json = new JSONSerializer();
			request.setAttribute("jsonString", json.include("roles").serialize(sysUser));
		}
		return "jsonString";
	}
	
	@RequestMapping("/json_del_sys_user")
	public String del(HttpServletRequest request,HttpServletResponse response){
		String ids = request.getParameter("ids");
		StringBuffer msg = new StringBuffer("{'success':'true'");
		if(StringUtils.isNotEmpty(ids)){
			String[] userIds = ids.split(",");
			msg.append(",'msg':'User ");
			for(String userId : userIds){
				if(StringUtils.isNotEmpty(userId)){
					SysUser readyToRemove = sysUserService.get(new Long(userId));
					msg.append(readyToRemove.getFirstName()).append(" "+readyToRemove.getLastName()).append(",");
					sysUserService.remove(readyToRemove);
				}
			}
			msg.deleteCharAt(msg.length()-1);
			msg.append(" had been remove!'");
		}
		msg.append("}");
		request.setAttribute("jsonString", msg);
		return "jsonString";
	}
	
	@RequestMapping("/login")
	public String login(HttpServletRequest request,HttpServletResponse response){
		String userName = request.getParameter("username");
		String password = request.getParameter("password");
		System.out.println(userName + password);
		SysUser loginUser= sysUserService.getUserByUserName(userName);
		StringBuffer buff = new StringBuffer("{");
		if(loginUser !=null){
			JSONSerializer json = new JSONSerializer();
			buff.append("\"loginResult\":\"success\",\"loginUser\":")
				.append(json.serialize(loginUser));
		}else{
			buff.append("\"loginResult\":\"failure\",\"msg\":\"Username is not exist.\"");
		}
		buff.append("}");
		request.setAttribute("jsonString", buff.toString());
		return "jsonString";
	}
}